var prefix = ctx + "monitor/cache";
layui.use(['layer', 'form', 'table', 'util', 'admin'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var util = layui.util;
    var admin = layui.admin;
    var selObj;  // 左表选中数据

    /* 渲染表格 */
    var insTb = table.render({
        elem: '#dictTable',
        url: prefix + "/getNames",
        height: 'full-95',
        toolbar: ['<p>',
            '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
            '</p>'].join(''),
        defaultToolbar: [],
        page: false,
        cols: [
            [
                {type: 'numbers'},
                {field: 'cacheName', title: '缓存名称'}
            ]
        ],
        done: function (res, curr, count) {
            $('#dictTable+.layui-table-view .layui-table-body tbody>tr:first').trigger('click');
        }
    });
    /* 表格搜索 */
    form.on('submit(dictTbSearch)', function (data) {
        insTb.reload({where: data.field});
        return false;
    });
    /* 表格头工具栏点击事件 */
    table.on('toolbar(dictTable)', function (obj) {
        if (obj.event === 'del') { // 删除
            var cacheName = selObj.data.cacheName;
            layer.confirm('确定要删除【' + cacheName + '】此缓存吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                admin.req(prefix + "/clearCacheName", {cacheName: cacheName}, function (result) {
                    layer.close(loadIndex);
                    himio.notice.resp(result, function () {
                        obj.del();
                        $('#dictTable+.layui-table-view .layui-table-body tbody>tr:first').trigger('click');
                    });
                }, 'post');
            });
        }
    });
    /* 监听行单击事件 */
    table.on('row(dictTable)', function (obj) {
        selObj = obj;
        obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
        insTb2.reload({
            where: {
                cacheName: obj.data.cacheName
            },
            page: {curr: 1},
            url: prefix + '/getKeys'
        });
    });


    /* 渲染表格2 */
    var insTb2 = table.render({
        elem: '#dictDataTable',
        data: [],
        page: false,
        // toolbar: ['<p>',
        //     '<button lay-event="clearAll" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>清空所有缓存</button>&nbsp;',
        //     '</p>'].join(''),
        toolbar:false,
        cols: [
            [
                {type: 'numbers'},
                {field: 'cacheKey', title: '缓存键名', sort: true},
                {title: '操作', toolbar: '#dictDataTbBar', align: 'center', minWidth: 200}
            ]
        ]
    });
    /* 表格2搜索 */
    form.on('submit(dictDataTbSearch)', function (data) {
        if (selObj) {
            data.field.cacheName = selObj.data.cacheName;
        }
        insTb2.reload({
            where: data.field,
            page: {curr: 1}
        });
        return false;
    });
    /* 表格2工具条（查看详细和删除单个）点击事件 */
    table.on('tool(dictDataTable)', function (obj) {
        if (obj.event === 'edit') { // 修改
            showEditModel2(obj.data);
        } else if (obj.event === 'del') { // 删除
            admin.confirm('确定要删除【' + obj.data.cacheKey + '】吗？', function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                if (!selObj || !selObj.data) {
                    himio.notice.warn("未选择缓存!");
                }
                admin.req(prefix + '/clearCacheKey', {
                    cacheName: selObj.data.cacheName,
                    cacheKey: obj.data.cacheKey
                }, function (res) {
                    layer.close(loadIndex);
                    himio.notice.resp(res, function () {
                        insTb2.reload({page: {curr: 1}});
                    });
                }, 'post');
            });
        }
    });
    /* 表格2头工具栏（清空缓存）点击事件 */
    table.on('toolbar(dictDataTable)', function (obj) {
        if (obj.event === 'clearAll') { // 删除
            admin.confirm('确定要清空所有缓存吗？', function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                admin.req(prefix + '/clearAll', function (res) {
                    layer.close(loadIndex);
                    himio.notice.resp(res, function () {
                        location.reload();
                    });
                }, 'get');
            });
        }
    });

    /* 显示表单弹窗2 */
    function showEditModel2(mData) {
        admin.open({
            type: 1,
            title: '查看缓存详细数据',
            area: '650px',
            content: $('#dictDataEditDialog').html(),
            success: function (layero, dIndex) {
                admin.req(prefix + "/getValue", {
                    cacheName: selObj.data.cacheName,
                    cacheKey: mData.cacheKey
                }, function (resp) {
                    if (resp.code === 0) {
                        // 回显表单数据
                        form.val('dictDataEditForm', resp.data);
                    } else {
                        himio.notice.error(resp.msg)
                    }
                }, 'post');

            }
        });
    }

});